package com.liaozibo.demo.seckill.support.export.dto;

import com.liaozibo.demo.seckill.support.export.constant.ResultConstants;
import lombok.Data;

import java.io.Serializable;
import java.util.Objects;

@Data
public class Result<T> implements Serializable {
    private String code;
    private String message;
    private T data;

    private Result() {}
    public static Result ok() {
        return ok(null);
    }

    public static <T> Result<T> ok(T data) {
        return ok(ResultConstants.CODE_SUCCESS, ResultConstants.MESSAGE_SUCCESS, data);
    }

    public static <T> Result<T> ok(String code, String message, T data) {
        Result<T> result = new Result<>();
        result.setCode(code);
        result.setMessage(message);
        result.setData(data);
        return result;
    }

    public static Result fail() {
        return fail(ResultConstants.CODE_FAIL, ResultConstants.MESSAGE_FAIL);
    }

    public static Result fail(String message) {
        return fail(ResultConstants.CODE_FAIL, message);
    }

    public static Result fail(String code, String message) {
        Result<Object> result = new Result<>();
        result.setCode(code);
        result.setMessage(message);
        return result;
    }

    public boolean isOk() {
        return Objects.equals(code, ResultConstants.CODE_SUCCESS);
    }

    public boolean isFail() {
        return Objects.equals(code, ResultConstants.CODE_FAIL);
    }
}
